﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_CD
{
    public partial class FromBaoCaoThucHienChuongTrinh1TySong : MyFromBase
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private static int IDDonVi;
        private static int KyBaoCao;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblNotice.Visible = false;
            CheckPermission();  
            if (!IsPostBack)
            {
                BindDataToDropDownList(); ;
                DataForTreeView.PopulateRootLevel(trvDonVi);
            }
            KyBaoCao = Convert.ToInt32(ddlKyBaoCao.SelectedValue);
        }

        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("KetQuaThucHienCongTac1TyDong");
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        protected void trvDonVi_SelectedNodeChanged(object sender, EventArgs e)
        {
            lblDonVi.Text = "Đơn vị đã chọn : " + trvDonVi.SelectedNode.Text;
            IDDonVi = Convert.ToInt32(trvDonVi.SelectedValue);
        }

        protected void trvDonVi_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }

        protected void BindDataToDropDownList()
        {
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM='KyBaoCao'");
            ddlKyBaoCao.DataSource = dt;
            ddlKyBaoCao.DataTextField = "TenDM";
            ddlKyBaoCao.DataValueField = "ID";
            ddlKyBaoCao.DataBind();
        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            if (trvDonVi.SelectedNode == null)
            {
                MyCommon.AddMessage(this.Page.Master, "Vui lòng lựa chọn đơn vị muốn xem báo cáo", MyConst.MessageType.Info);
            }
            else
            {

                List<SqlParameter> lstParams = new List<SqlParameter>();
                lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                MyReportSQL = "SELECT * FROM dbo.tblBaoCaoKetQuaThucHienChuongTrinh1TyDong WHERE KyBaoCaoID_DM = @KyBaoCao AND DonViID = @DonViID";
                MyReportParams = lstParams;
                MyReportSQLParam = "SELECT  BC.CM_TenBaoCao,BC.CM_ThuTruong,BC.CM_NguoiTongHop,TenDV AS TenDonVi " +
                                   "FROM dbo.tblBaoCaoKetQuaThucHienChuongTrinh1TyDong BC " +
                                   "INNER JOIN dbo.tblDonVi DV ON DV.ID = BC.DonViID " +
                                   "WHERE BC.KyBaoCaoID_DM = @KyBaoCao AND BC.DonViID = @DonViID";
                MyReportName = "rptBaoCaoKetQuaThucHienCongTac1TyDong.rpt";
                MyTableName = "tblBaoCaoKetQuaThucHienChuongTrinh1TyDong";

                this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "window.open('MyReportView.aspx');", true);


            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}
